package mhys.Util;

import java.sql.*;

/*
    连接数据库
*/
public class DBUtil {
    //1.注册驱动
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    //2.获取连接
    public static Connection getConn() throws SQLException {
        return DriverManager.getConnection("jdbc:mysql:///studb?useSSL=false&&characterEncoding=utf-8&&serverTimezone=Asia/Shanghai","root","root");
    }
    //3.关闭资源
    public static void close(ResultSet res, PreparedStatement stmt, Connection conn){

        if (res !=null){
            try {
                res.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
